<?php
//
//	file: install_wpu/install.php
//	begin: 08/05/2007
//	$Author: christopher $
//	$Revision: 14 $
//	$Date: 2007-10-08 14:55:03 +0000 (Mon, 08 Oct 2007) $
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)

define('IN_PHPBB', true);
define('IN_INSTALL', true);

$phpbb_root_path = './../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
$requester = 'install_title/install';

include($phpbb_root_path . 'common.'.$phpEx);
include($config->url('includes/class_install'));
include($config->url('includes/class_cp'));
include($config->url('includes/wpu/class_wpu'));

// script def
$requester = 'install_wpu/' . basename(__FILE__, '.' . $phpEx);

// constants
define('ROWS_A_TURN', 500);

// Old attachmod tables

// tpls
function welcome_form()
{
	global $page;
?><form name="post" method="post" action="<?php echo $page->url(); ?>"><br /><br /><div align="center"><table cellpadding="4" cellspacing="1" border="0" width="80%" class="background">
<tr><th><?php echo sprintf($page->lang('WP_welcome'), WPU_CURRENT_VERSION); ?></th></tr>
<tr><td align="justify" class="row1"><?php echo sprintf($page->lang('WP_welcome_explain'), WPU_CURRENT_VERSION); ?><br /><br /><br /></td></tr>
<tr><td align="center" class="row2"><?php echo $page->lang('WP_start'); ?>: <input name="submit" type="submit" value="<?php echo $page->lang('WP_proceed'); ?>" /></td></tr>
</table></div><?php $page->hide(); ?></form>
<?php
}

// steps
$steps = array(
	'WP_welcome',
	'WP_previous_version',
	'WP_db',
	'WP_wizard',
	'WP_patch',
	'WP_end',
);

// parms (not set is : type => TYPE_INT, default => 0)
$parms_def = array(
	'step' => array('list' => &$steps),
	'wppv' => array('type' => TYPE_NO_HTML, 'default' => ''),
	'db' => '',
	'wpwiz' => array('type' => TYPE_INT, 'default' => ''),
	'wpwizd' => '',
	'wpp' => '',
);

// step functions
function step()
{
	global $parms, $steps;
	return $steps[ $parms['step'] ];
}

function next_step($step='')
{
	global $page, $parms, $steps;
	if ( !empty($step) )
	{
		$t_steps = array_flip($steps);
		$parms['step'] = $t_steps[$step];
	}
	else
	{
		$parms['step']++;
	}
	$page->set_parms($parms);
}

//--------------------------------------
//
// Start the process
//
//--------------------------------------

// parms reading
$parms = array();
foreach ( $parms_def as $parm => $data )
{
	$type = empty($data) || !isset($data['type']) ? TYPE_INT : $data['type'];
	$default = empty($data) || !isset($data['default']) ? (($type == TYPE_INT) ? 0 : '') : $data['default'];
	$list = empty($data) || !isset($data['list']) ? '' : $data['list'];
	$parms[$parm] = _read($parm, $type, $default, $list);
}

// start
$page = new page($requester, '', 'lang_CH_install_wpu');
$page->set_parms($parms);
$page->sub_title = '';

// set version
$lang['Script_title'] = sprintf($lang['Script_title'], WPU_CURRENT_VERSION);

// log in
$session = new light_session();
$session->log_in(ADMIN);

// send welcome
if ( step() == 'WP_welcome' )
{
	$page->header();
	next_step();
	welcome_form();
	$page->footer();
}

// detect wpu mod
if ( step() == 'WP_previous_version' )
{
	$parms['wppv'] = '';
	$sql = 'SELECT config_value
				FROM ' . CONFIG_TABLE . '
				WHERE config_name = \'mod_wp_united\'';
	$result = $db->sql_query($sql, false, __LINE__, __FILE__);
	if ( $row = $db->sql_fetchrow($result) )
	{
		$parms['wppv'] = $row['config_value'];
	}
	$db->sql_freeresult($result);

	next_step();
}

// display previous version
if ( !empty($parms['wppv']) )
{
	$page->output(sprintf($page->lang('WP_previous_version'), $parms['wppv']));
}

// -----------------------------------------
//
// create or upgrade the database structure
//
// -----------------------------------------
if ( step() == 'WP_db' )
{
	// get the db install layer
	if ( !($sql_layer = $db->get_layer()) )
	{
		$page->critical_error('WP_db_not_supported');
	}
	if ( $sql_layer['SCHEMA'] == 'postgres' )
	{
		$sql_layer['SCHEMA'] = 'pgsql';
	}
	$dbi_layer = 'dbi_' . $sql_layer['SCHEMA'];

	// include the layers
	include($config->url('includes/class_xml'));
	include($config->url('includes/db/class_dbi'));
	include($config->url('includes/db/class_' . $dbi_layer));

	// read the sql instructions
	$schemas = array();
	switch ( $parms['wppv'] )
	{
		case '0.9.2':
			$schemas[] = 'schema_092';
			break;
		default:
			$schemas[] = 'schema_ch';
			break;
	}

	// process the schemas
	$xml_parser = new xml_parser();
	$dbi = new $dbi_layer($table_prefix);
	$count_schemas = count($schemas);
	for ( $i = 0; $i < $count_schemas; $i++ )
	{
		// read & parse the XML schema
		$actions = false;
		if ( ($xml_name = phpbb_realpath($phpbb_root_path . 'install_wpu/schemas/' . $schemas[$i] . '.xml')) && ($handler = @fopen($xml_name, 'r')) )
		{
			$xml = trim(fread($handler, filesize($xml_name)));
			fclose($handler);
			unset($handler);
			$actions = empty($xml) ? false : $xml_parser->parse($xml);
			unset($xml);
			if ( $actions === false )
			{
				$page->critical_error($xml_parser->errmsg);
			}
		}

		// process the sqls
		if ( $actions && isset($actions['cdata']) && ($count_actions = count($actions['cdata'])) )
		{
			for ( $j = 0; $j < $count_actions; $j++ )
			{
				$sqls = isset($actions['cdata'][$j]['actions']) ? $dbi->process($actions['cdata'][$j]['actions']) : false;
				unset($actions['cdata'][$j]);

				$count_sqls = $sqls ? count($sqls) : 0;
				for ( $k = 0; $k < $count_sqls; $k++ )
				{
					$result = $db->sql_query($sqls[$k], false, __LINE__, __FILE__, false);
					if ( $result === false )
					{
						$sql_error = $db->sql_error();
						$page->error(sprintf($page->lang('WP_sql_already_done'), ($sql_error['message'] ? $sql_error['message'] . ': ' : '') . $sqls[$k]));
					}
					else if ( $result !== true )
					{
						$db->sql_freeresult($result);
					}
					unset($result);
				}
				unset($sqls);
			}
		}
		unset($actions);
	}
	unset($dbi);
	unset($xml_parser);
	unset($schemas);

	// jump to end if not a migration from log mod
	$parms['db'] = true;
	next_step();
	$page->loop();
}
if ( !empty($parms['db']) )
{
	$page->output(empty($parms['wppv']) ? 'WP_db_done' : 'WP_db_upgraded');
}

// --------------------------
//
// install wizard
//
// --------------------------
if ( step() == 'WP_wizard' )
{
	include($config->url('install_wpu/install_inc'));
	$wp_wizard = new wp_wizard();
	$wp_wizard->process($parms['wpwiz']);
	if ( !empty($parms['wpwizd']) )
	{
		next_step();
	}
}

// --------------------------
//
// finalize the installation
//
// --------------------------
if ( step() == 'WP_patch' )
{
	$config->set('mod_wp_united', WPU_CURRENT_VERSION, true);

	// instantiate the acting user
	$sav_lang = $lang;
	$user = new user();
	$user->read($session->user_id);

	// patch the panels
	$cp_panels = new cp_panels($requester);
	$cp_panels->read(true);
	$cp_panels->patch(true);

	// remove the acting user
	unset($user);
	$lang = $sav_lang;

	$parms['wpp'] = true;
	next_step();
}
if ( !empty($parms['wppp']) )
{
	$page->output('WP_panels_patched');
}

// all is done, recache
if ( step() == 'WP_end' )
{
	$sql = 'DELETE FROM ' . USERS_CACHE_TABLE;
	$db->sql_query($sql, false, __LINE__, __FILE__);

	$page->error(empty($parms['wppv']) ? 'WP_install_done' : 'WP_install_upgraded');
	$page->critical_error('WP_return_to_board');
}

// send page
$page->header();
$page->footer();

?>